<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
import resize from '../mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '350px',
    },
    autoResize: {
      type: Boolean,
      default: true,
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        color: ['rgb(90,177,239)'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999',
            },
          },
        },
        toolbox: {
          x: 'right',
          bottom: 20,
          feature: {
            // dataView: { show: true, readOnly: false },
            // magicType: { show: true, type: ['line', 'bar'] },
            // restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 60,
          top: 60,
          containLabel: true,
        },
        legend: {
          left: 'center',
          bottom: 20,
          data: ['实际完成工单数', '完成率'],
        },
        color: ['rgb(105,161,249)', 'rgb(89,205,170)'],
        xAxis: [
          {
            type: 'category',
            data: [
              '2022-2-22',
              '2022-2-23',
              '2022-2-24',
              '2022-2-25',
              '2022-2-26',
              '2022-2-27',
              '2022-2-28',
            ],
            axisPointer: {
              type: 'shadow',
            },
            axisLine: {
              lineStyle: {
                color: '#000',
              },
            },
          },
        ],
        yAxis: [
          {
            type: 'value',
            name: '数量',
            min: 0,
            max: 250,
            interval: 50,
            axisLabel: {
              formatter: '{value}',
            },
            axisLine: {
              lineStyle: {
                color: '#000',
              },
            },
          },
          {
            type: 'value',
            name: '应收款',
            min: 0,
            max: 25,
            interval: 5,
            axisLabel: {
              formatter: '{value}万',
            },
            axisLine: {
              lineStyle: {
                color: '#000',
              },
            },
          },
        ],
        series: [
          {
            name: '实际完成工单数',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value
              },
            },
            data: [
              2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4,
              3.3,
            ],
          },
          {
            name: '完成率',
            type: 'line',
            yAxisIndex: 1,
            tooltip: {
              valueFormatter: function (value) {
                return value + ' %'
              },
            },
            data: [
              2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2,
            ],
          },
        ],
      })
    },
  },
}
</script>
